//获取管理员列表
var table = $("#staff_List").dataTable({
    //"iDisplayLength":10,
    "ajax": {
        "url":"/system/get-sys-leve-list",
        "type":'post',
        "data": function (d) {
            //添加额外的参数传给服务器
            //return $.extend({ CurrentPage:0, PageSize: 10}, {});
        }},
    'bPaginate': true,
    "bDestory": true,
    "bRetrieve": true,
    "bFilter":false,
    "bSort": true,
    "bProcessing": true,
    "autoWidth": false,
    "searching":true,
    "aoColumns": [
        {"mDataProp": "LevelId",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html(iRow+1);
            }
        },
        {"mDataProp": "LevelNo"},
        {"mDataProp": "LevelName"},
        {"mDataProp": "BrokerageRatio",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html(oData.BrokerageRatio+'%');
            }
        },
        {"mDataProp": "UpScore"},
        {"mDataProp": "LevelDescription"},
        {
            "mDataProp": "LevelId",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html('<button type="button" class="btn btn-info btn-xs EditLevel"   data-levelid="'+oData.LevelId+'"  data-levelname="'+oData.LevelName+'"  data-brokerageratio="'+oData.BrokerageRatio+'"  data-upscore="'+oData.UpScore+'" data-leveldescription = "'+oData.LevelDescription+'"><i class="fa fa-edit"></i> 编辑</button>')
            }
        },
    ],
    "order": [[0, "desc" ]],
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
        //add selected class
        $(nRow).click(function () {
            if ($(this).hasClass('row_selected')) {
                $(this).removeClass('row_selected');
            } else {
                table.$('tr.row_selected').removeClass('row_selected');
                $(this).addClass('row_selected');
            }
        });
    },
    "fnInitComplete": function (oSettings, json) {
    }
});


//点击编辑
$(document).on('click','.EditLevel',function(){
    var LevelId = $(this).data("levelid");
    var LevelName = $(this).data("levelname");
    var BrokerageRatio = $(this).data("brokerageratio");
    var UpScore = $(this).data("upscore");
    var LevelDescription = $(this).data("leveldescription");
    $("#LevelName").val(LevelName);
    $("#BrokerageRatio").val(BrokerageRatio);
    $("#UpScore").val(UpScore);
    $("#LevelDescription").val(LevelDescription);
    $("#EditForm").attr("data-levelid",LevelId);
    $("#EditLevel").modal('show');
});

//编辑
function EditLevel(){
    var JsonData = {
        LevelId:$("#EditForm").data("levelid"),
        LevelName: $("#LevelName").val(),
        BrokerageRatio: $("#BrokerageRatio").val(),
        UpScore: $("#UpScore").val(),
        LevelDescription: $("#LevelDescription").val()
    }
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/system/level-update',
        data: JsonData,
        success: function (ret) {
            $("#EditLevel").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#staff_List').DataTable();
                table.ajax.reload();
                layer.msg('修改成功！');
            }else{
                layer.msg('修改失败！');
            }
        }
    });
}


//隐藏模态窗口
$("#EditLevel").on("hide.bs.modal", function () {
    setTimeout(function(){
        resetFrom();
    },100)

});
$("#EditForm").validate({
    submitHandler:function() {
        EditLevel();
    },
    rules:{
        BrokerageRatio:{
            Decimal:true
        },
        UpScore:{
            digits:true
        }
    },
    messages: {
        BrokerageRatio: {
            Decimal:'请正确输入佣金比例'
        },
        UpScore: {
            digits:'请输入正整数'
        }

    }
});
//检测用户姓名是否为汉字
jQuery.validator.addMethod('Decimal', function(value, element) {
    var length = value.length;
    var regName = /^[0-9]{1}\d*(.\d{1,2})?$|^0.\d{1,2}$/;
    return this.optional(element) || regName.test( value );
});


//重置表单
function resetFrom() {
    $('form').each(function (index) {
        $('form')[index].reset();
    });
}